/*
 * @Author: your name
 * @Date: 2020-04-13 11:48:52
 * @LastEditTime: 2020-06-28 18:06:55
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \467\huosdk_web_js_css\vue_wap\vue.config.js
 */
// v8static.huosdk.com/
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
  .BundleAnalyzerPlugin;
const path = require("path");
const IS_PROD = ["production"].includes(process.env.NODE_ENV);
const filenameHashing = true;
const productionSourceMap = !IS_PROD;
const assetsDir = "";
function resolve(dir) {
  return path.join(__dirname, dir);
}
function getAssetPath(assetsDir, filePath) {
  return assetsDir ? path.posix.join(assetsDir, filePath) : filePath;
}
let baseStaticUrl = "/static/wap/dist";

console.log(process.env.NODE_ENV);

module.exports = {
  publicPath: process.env.NODE_ENV === "development" ? "/" : baseStaticUrl,
  devServer: {
    open: true,
    hotOnly: true,
    port: 4003,
    proxy: {
      "/api": {
        target: "https://test-m.kejingou.cn/",
        changeOrigin: true,
        pathRewrite: {
          "^/api": ""
        }
      }
    }
  },
  chainWebpack: config => {
    config.resolve.symlinks(true); // 修复热更新失效
    config.resolve.alias
      .set("@$", resolve("src"))
      .set("assets", resolve("src/assets"))
      .set("components", resolve("src/components"))
      .set("views", resolve("src/views"))
      .set("lib", resolve("src/lib"))
      .set("config", resolve("src/config"))
      .set("router", resolve("src/router"))
      .set("view", resolve("src/view"))
      .set("store", resolve("src/store"));
    // 生产环境去除资源预加载及引入文件路径带双引号
    // 打包分析
    if (process.env.IS_ANALYZ) {
      config
        .plugin("webpack-report")
        .use(BundleAnalyzerPlugin, [{ analyzerMode: "static" }]);
    }
    // 制定环境打包js路径
    if (
      process.env.NODE_ENV === "production" ||
      process.env.NODE_ENV === "test"
    ) {
      const isLegacyBundle =
        process.env.VUE_CLI_MODERN_MODE && !process.env.VUE_CLI_MODERN_BUILD;
      const timestamp = new Date().getTime();
      const filename = getAssetPath(
        assetsDir,
        `js/[name]${isLegacyBundle ? `-legacy` : ``}${
          filenameHashing ? ".[contenthash:8]" : ""
        }.${timestamp}.js`
      );
      config
        .mode("production")
        .devtool(productionSourceMap ? "source-map" : false)
        .output.filename(filename)
        .chunkFilename(filename);
      config.optimization.minimize(true);
    }
  }
};
